package com.wanxing.main;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import sim.Sim;

import com.zhny.wxginger.R;
import com.zhny.handler.Sendcommand;
import com.util.HttpUtils;
import com.util.Info;

@SuppressLint("NewApi")
public class ControlFragmentNew extends Fragment implements OnClickListener {
	private String DTU = null;
	private Handler handler;
	private ArrayAdapter<String> lab_adapter;
	private boolean flag = false;
	private Boolean s_switch = false;

	// Button b2;// ����
	// Button b3;// ����
	private Button c_s_open;
	private Button c_s_close;
	private Button c_f_open;
	private Button c_f_close;
	private Button c_z_open;
	private Button c_z_close;
	private Button c_all_open;
	private Button c_all_close;
	private Button c_feng_open;
	private Button c_feng_close;

	// Switch myswitch;

	// @Override
	// protected void onCreate(Bundle savedInstanceState) {
	// super.onCreate(savedInstanceState);
	// setContentView(R.layout.control_set);
	// System.out.println("�л��ɹ�");
	// viewFind();
	// viewSet();
	// ActionBar actionBar = getActionBar();
	// actionBar.setDisplayShowTitleEnabled(false);
	// actionBar.setDisplayShowHomeEnabled(false);
	// }

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater
				.inflate(R.layout.control_set_new, container, false);
		viewFind(view);
		// viewSet();
		String labname = getActivity().getActionBar().getTitle().toString();
		DTU = labname;
		return view;
	}

	public void viewFind(View view) {
		c_s_open = (Button) view.findViewById(R.id.c_s_open);
		c_s_close = (Button) view.findViewById(R.id.c_s_close);
		c_f_open = (Button) view.findViewById(R.id.c_f_open);
		c_f_close = (Button) view.findViewById(R.id.c_f_close);
		c_z_open = (Button) view.findViewById(R.id.c_z_open);
		c_z_close = (Button) view.findViewById(R.id.c_z_close);
		c_all_open = (Button) view.findViewById(R.id.c_all_open);
		c_feng_open = (Button) view.findViewById(R.id.c_fengshan_open);
		c_feng_close = (Button) view.findViewById(R.id.c_fengshan_close);
		
		c_s_open.setOnClickListener(this);
		c_s_close.setOnClickListener(this);
		c_f_open.setOnClickListener(this);
		c_f_close.setOnClickListener(this);
		c_z_open.setOnClickListener(this);
		c_z_close.setOnClickListener(this);
		c_all_open.setOnClickListener(this);
		
		c_feng_open.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				String message = getActivity().getResources().getText(R.string.openmessage).toString().trim();
				Sim sim = new Sim(message);
				sim.sendMessage();
				Toast.makeText(getActivity(), "�ѳɹ����Ͷ���--��", 1).show();
			}
		});
		c_feng_close.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				String message = getActivity().getResources().getText(R.string.closemessage).toString().trim();
				Sim sim = new Sim(message);
				sim.sendMessage();
				Toast.makeText(getActivity(), "�ѳɹ����Ͷ���--�ر�", 1).show();
			}
		});

	}

	private void showDialog(String str) {
		AlertDialog.Builder builder;
		builder = new AlertDialog.Builder(this.getActivity());
		builder.setTitle("��ʾ��");
		builder.setMessage(str);
		builder.setPositiveButton("ȷ��", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface arg0, int arg1) {
				// TODO Auto-generated method stub
			}
		});
		AlertDialog dialog = builder.create();
		dialog.show();
	}

	private void showDialog1(final int split) {
		LayoutInflater inflater = LayoutInflater.from(this.getActivity());
		final View view = inflater.inflate(R.layout.dialog_item, null);
		AlertDialog.Builder builder;
		builder = new AlertDialog.Builder(this.getActivity());
		builder.setTitle("���������������֤");
		builder.setView(view);
		builder.setPositiveButton("�ύ", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface arg0, int arg1) {
				// TODO Auto-generated method stub
				EditText id = (EditText) view.findViewById(R.id.ed_id);
				String sid = id.getText().toString();
				if (sid.equals("112233")) {
					// ����ָ��
					String command;
					switch (split) {
					case 1:
						command = "BB00dezhoulx" + "006" + "AE";// ˮ�õ����
						System.out.println(split + "  " + command);
						new Thread(new Sendcommand(handler, command)).start();
						break;
					case 2:
						command = "BB00dezhoulx" + "006" + "AF";// �ر�
						System.out.println(split + "  " + command);
						new Thread(new Sendcommand(handler, command)).start();
						break;
					case 3:
						command = "BB00dezhoulx" + DTU + "AC";// ���ϵ����
						System.out.println(split + "  " + command);
						new Thread(new Sendcommand(handler, command)).start();
						break;
					case 4:
						command = "BB00dezhoulx" + DTU + "AD";// �ر�
						System.out.println(split + "  " + command);
						new Thread(new Sendcommand(handler, command)).start();
						break;
					case 5:
						command = "BB00dezhoulx" + DTU + "AA";// ��ˮ�ܵ�ط���
						System.out.println(split + "  " + command);
						new Thread(new Sendcommand(handler, command)).start();
						break;
					case 6:
						command = "BB00dezhoulx" + DTU + "AB";// �ر�
						System.out.println(split + "  " + command);
						new Thread(new Sendcommand(handler, command)).start();
						break;
					case 7:

						if (DTU.equals("005")) {

							command = "BB00dezhoulx" + DTU + "FA";// �ֶ���ˮʩ�ʴ�  - 6����
							System.out.println(split + "  " + command);
							new Thread(new Sendcommand(handler, command))
									.start();
						} else {
							command = "BB00dezhoulx" + "006" + "FB";// �ֶ���ˮʩ�ʴ�  ��ˮ�� - ������
							System.out.println(split + "  " + command);
							new Thread(new Sendcommand(handler, command))
									.start();
							command = "BB00dezhoulx" + DTU + "FA";// �ֶ���ˮʩ�ʴ� �򿪷��� - ������
							System.out.println(split + "  " + command);
							new Thread(new Sendcommand(handler, command))
									.start();
						}
						break;
					}
				} else {
					Toast.makeText(getActivity(), "��֤ʧ��", 1).show();
				}
			}
		});
		builder.setNegativeButton("ȡ��", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface arg0, int arg1) {
				// TODO Auto-generated method stub
			}
		});
		AlertDialog dialog = builder.create();
		dialog.setCanceledOnTouchOutside(false);
		dialog.show();

	}

	public void viewSet() {

		/*
		 * b2.setOnClickListener(new View.OnClickListener() {
		 * 
		 * @Override public void onClick(View v) { // flag = false; Intent
		 * intent = new Intent(getActivity(), Dialog_water_Activity.class);
		 * 
		 * intent.putExtra("dtu", DTU); startActivity(intent);
		 * 
		 * } });
		 * 
		 * b3.setOnClickListener(new OnClickListener() {
		 * 
		 * @Override public void onClick(View arg0) { // TODO Auto-generated
		 * method stub //showDialog3(); Intent intent = new
		 * Intent(getActivity(), Dialog_time_Activity.class);
		 * startActivity(intent); } });
		 */
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		String command;
		int p = 0;
		switch (v.getId()) {
		case R.id.c_s_open:
			p = 1;
			showDialog1(p);
			break;
		case R.id.c_s_close:
			p = 2;
			showDialog1(p);
			break;
		case R.id.c_f_open:
			p = 3;
			showDialog1(p);
			break;
		case R.id.c_f_close:
			p = 4;
			showDialog1(p);
			break;
		case R.id.c_z_open:
			p = 5;
			showDialog1(p);
			break;
		case R.id.c_z_close:
			p = 6;
			showDialog1(p);
			break;
		case R.id.c_all_open:
			p = 7;
			showDialog1(p);
			break;
		}

	}
	/*
	 * class myAsyncTask2 extends AsyncTask<String, Void, String> {
	 * 
	 * @Override protected String doInBackground(String... params) { // TODO
	 * Auto-generated method stub String result =
	 * HttpUtils.sendPostMethod(params[0], "utf-8");
	 * System.out.println(result+"--------get ˮ�õ��"); return result; }
	 * 
	 * @Override protected void onPostExecute(String result) { // TODO
	 * Auto-generated method stub super.onPostExecute(result);
	 * 
	 * if (result == null) { myswitch.setChecked(false); s_switch = false; }
	 * else if (result.equals("10.00")) { myswitch.setChecked(true); s_switch =
	 * true; } else if (result.equals("00.00")) { myswitch.setChecked(false);
	 * s_switch = false; } }
	 * 
	 * }
	 */

}
